package viroyal.com.base.activity.exam;

import android.os.Bundle;

import com.hwangjr.rxbus.RxBus;
import com.suntiago.network.network.Api;
import com.suntiago.network.network.BaseRspObserver;
import com.suntiago.network.network.ErrorCode;

import org.kymjs.kjframe.KJDB;

import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import viroyal.com.base.activity.main.MainActivity;
import viroyal.com.base.common.ConstantsYJ;
import viroyal.com.base.net.ApiObserver;
import viroyal.com.base.net.entity.Exam;
import viroyal.com.base.net.rsp.ExamResponse;
import viroyal.com.dev.NFCMonitorBaseActivity;
import viroyal.com.dev.splash.ConfigDevice;



/**
 * @author chenjunwei
 * @desc 考试模式
 * @date 2019/4/30
 */
public class ExamActivity extends NFCMonitorBaseActivity<ExamDelegate, ExamModel> {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    viewDelegate.initWidget();
  }

  @Override
  protected Class<ExamDelegate> getDelegateClass() {
    return ExamDelegate.class;
  }

  @Override
  protected Class<ExamModel> getModelClass() {
    return ExamModel.class;
  }

  @Override
  protected void initView(Bundle savedInstanceState) {
    super.initView(savedInstanceState);
    syncExamInfo();
  }

  /**
   * 同步考试
   */
  private void syncExamInfo() {
    loadExamInfo();
    Subscription syncExamInfo = Observable.interval(ConstantsYJ.TIME_SYNC_EXAM_INFO_DELAY_MS,
            TimeUnit.MILLISECONDS)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(aLong -> {
              loadExamInfo();
            });

    addRxSubscription(syncExamInfo);
  }

  private void loadExamInfo() {
    Subscription loadExamInfo = Api.get().getApi(ApiObserver.class)
            .exam(ConfigDevice.getDeviceId(), ConfigDevice.school_id)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new BaseRspObserver<>(ExamResponse.class, rsp -> {
              if (ErrorCode.isSuccess(rsp.error_code)) {
                if (null != rsp.data && rsp.data.size() > 0) {
                  //删除保存考试信息
                  KJDB.getDefaultInstance().deleteByWhere(Exam.class, "1==1");
                  KJDB.getDefaultInstance().save(rsp.data);
                  loadExamInfoCallBack(rsp.data);
                } else {
                  finish();
                }
              } else {
                List<Exam> examList = KJDB.getDefaultInstance().findAllByWhere(Exam.class, "1==1");
                if (null != examList && examList.size() > 0) {
                  loadExamInfoCallBack(examList);
                } else {
                  finish();
                }
              }
            }));
    addRxSubscription(loadExamInfo);
  }

  private void loadExamInfoCallBack(List<Exam> examList) {
    long time = System.currentTimeMillis();
    for (Exam item : examList) {
      if ((item.starttime) <= time && time < (item.endtime)) {
        viewDelegate.updateExamView(item);
        return;
      }
    }
    finish();
  }

  @Override
  protected void onDestroy() {
    RxBus.get().post(MainActivity.EXIT_EXAM, "");
    super.onDestroy();
  }
}
